import jieba
from collections import Counter
import matplotlib.pyplot as plt

# 示例文本，实际操作时请替换为你的语料库内容
with open('./jyxstxtqj_downcc/笑傲江湖.txt', 'r', encoding='gb18030') as f:
    text = f.read()

# 分词
words = jieba.cut(text)

# 统计词频
word_counts = Counter(words)

# 排序
sorted_word_counts = sorted(word_counts.items(), key=lambda x: x[1], reverse=True)

# 绘制图表
ranks = range(1, len(sorted_word_counts) + 1)
frequencies = [freq for _, freq in sorted_word_counts]
plt.figure(figsize=(10, 6))
plt.loglog(ranks, frequencies, marker="o")
plt.xlabel('Rank')
plt.ylabel('Frequency')
plt.title('Zipf\'s Law')
plt.show()
